package Model;

/*CREATE  TABLE IF NOT EXISTS `mydb`.`Perifericos` (
  `idPerifericos` INT NULL AUTO_INCREMENT ,
  `nome` VARCHAR(100) NULL ,
  `descricao` VARCHAR(250) NULL ,
  PRIMARY KEY (`idPerifericos`) )
ENGINE = InnoDB
COMMENT = 'Mouse; Teclado; Monitor; etc.';*/
public class Perifericos {
    //<editor-fold defaultstate="collapsed" desc="Connection">
    private static com.mysql.jdbc.Connection connection;

    public static com.mysql.jdbc.Connection getConnection() {
        return connection;
    }

    public static void setConnection(com.mysql.jdbc.Connection connection) {
        Perifericos.connection = connection;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Variable Declaration">
    private int idPerifericos;
    private String nome;
    private String descricao;
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Constructor">
    public Perifericos(int idPerifericos) {
        try {
            com.mysql.jdbc.ResultSet rs = (com.mysql.jdbc.ResultSet) connection.createStatement().executeQuery("SELECT * FROM itens WHERE idPerifericos = '" + idPerifericos + "'");
            rs.next();
            this.idPerifericos = rs.getInt("idPerifericos");
            this.nome = rs.getString("nome");
            this.descricao = rs.getString("descricao");
        } catch (Exception e) {
        }
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="toString">
    @Override
    public String toString() {
        String s="Perifericos:";
            s += "idPerifericos=" + idPerifericos;
        return s;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Getter">
    public int idPerifericos() {
        return this.idPerifericos;
    }

    public String nome() {
        return this.nome;
    }

    public String descricao() {
        return this.descricao;
    }

    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Setter">
    public boolean idPerifericos(int idPerifericos) {
        boolean ok = update("idPerifericos", idPerifericos);
        if (ok) {
            this.idPerifericos = idPerifericos;
        }
        return ok;
    }
    public boolean nome(String nome) {
        boolean ok = update("nome", nome);
        if (ok) {
            this.nome = nome;
        }
        return ok;
    }
    public boolean descricao(String descricao) {
        boolean ok = update("descricao", descricao);
        if (ok) {
            this.descricao = descricao;
        }
        return ok;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Update">
    /**
     *
     * @param campo
     * @param valor
     * @return
     */
    public boolean update(String campo, Object valor) {
        try {
            String sql = "UPDATE Perifericos \n"
                    + "SET " + campo + " = '";
            if (valor instanceof String) {
                sql += (String) valor;
            } else if (valor instanceof Integer) {
                sql += (Integer) valor;
            } else if (valor instanceof java.sql.Timestamp) {
                sql += ((java.sql.Timestamp) valor).toString().split("\\.")[0];
            } else if (valor instanceof java.util.Date) {
                java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
                        "yyyy-MM-dd hh:mm:ss");
                sql += sdf.format((java.util.Date) valor);
            } else {
                sql += valor.toString();
            }
            sql += "'\n WHERE \n idPerifericos = '" + idPerifericos + "'";
            com.mysql.jdbc.Statement st =
                    (com.mysql.jdbc.Statement) connection.createStatement();
            int x = st.executeUpdate(sql);
            if (x == 1) {
                return true;
            }
        } catch (Exception ex) {
        }
        return false;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Search">
    public static java.util.ArrayList<Perifericos> searchIdPerifericos(int valor) {
        return search("idPerifericos", valor+"");
    }
    public static java.util.ArrayList<Perifericos> searchNome(String valor) {
        return search("nome", valor+"");
    }
    public static java.util.ArrayList<Perifericos> searchDescricao(String valor) {
        return search("descricao", valor+"");
    }

    public static java.util.ArrayList<Perifericos> search(String campo, String valor) {
        try {
            java.util.ArrayList<Perifericos> retorno = new java.util.ArrayList<>();
            String sql = "SELECT idPerifericos FROM Perifericos "
                    + "WHERE " + campo + " LIKE '%" + valor + "%' "
                    + "ORDER BY " + campo;
            com.mysql.jdbc.ResultSet rs = (com.mysql.jdbc.ResultSet) connection.createStatement().executeQuery(sql);
            while (rs.next()) {
                retorno.add(new Perifericos(rs.getInt("idPerifericos")));
            }
            return retorno;
        } catch (Exception e) {
        }
        return null;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="New">
    public static Perifericos newPerifericos(int idPerifericos, String nome, String descricao){
            String sql = "INSERT INTO Perifericos (idPerifericos, nome, descricao)"
                    + " VALUES "
                    + "('"+idPerifericos+"', '"+nome+"', '"+descricao+"');";
        com.mysql.jdbc.Statement stmt = null;
        com.mysql.jdbc.ResultSet rs = null;
        try {
            stmt = (com.mysql.jdbc.Statement) connection.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY,
                    java.sql.ResultSet.CONCUR_UPDATABLE);
            stmt.executeUpdate(sql, com.mysql.jdbc.Statement.RETURN_GENERATED_KEYS);
            rs = (com.mysql.jdbc.ResultSet) stmt.getGeneratedKeys();
            if (rs.next()) {
                return new Perifericos(rs.getInt("idPerifericos"));
            } else {
                return new Perifericos(idPerifericos);
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception ex) {
                    // ignore
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception ex) {
                    // ignore
                }
            }
        }
        return null;
    }
    //</editor-fold>
}